/**
 * base js
 */
var _baseEvent = function(){
	"use strict";
	this._baseEventPool = {};
	
	this.setBaseEvent = function(eventName,event){
		this._baseEventPool[eventName] = event;
		return this;
	}
	
	this.runBaseEvent = function(eventName){
		if(this.hasBaseEvent(eventName)){
			var eventFun = this._baseEventPool[eventName];
			var _arguments = '';
			var _argumentIndex = [];
			var _argumentValue = [];
			if(typeof arguments[1] != 'undefined' && arguments[1].constructor == Array){
				for(var argumentId in arguments[1]){
					_argumentValue[argumentId] = arguments[1][argumentId];
					_argumentIndex[argumentId] = '_argumentValue['+argumentId+']';
				}
				var _arguments = _argumentIndex.join(',');
			}
			var runCommand = 'eventFun('+_arguments+')';
			return eval(runCommand);
		}
		return undefined;
	}
	
	this.hasBaseEvent = function(eventName){
		return this._baseEventPool.hasOwnProperty(eventName);
	}
	
}

var _baseComponent = function(){
	"use strict";
	_baseEvent.apply(this);	
	_baseHash.apply(this);
	this.component = function(name,component){
		this[name] = new component;
	}
}

var _baseHash = function(){
	this._baseHashData = {}
	
	this.setHash = function(key,value){
		return this._baseHashData[key] = value;
	}
	
	this.getHash = function(key){
		return this._baseHashData[key];
	}
	
	this.hasHash = function(key){
		return this._baseHashData.hasOwnProperty(key);
	}
	
	this.delHash = function(key){
		delete this._baseHashData[key];
	}
	
	this.clearHash = function(){
		return this._baseHashData = {}
	}
	
	this.getHashAll = function(){
		return this._baseHashData;
	}
	
}

/**
 * client js
 */
var _client = function(){
	_baseComponent.apply(this);
}

client = (function(obj){
	"use strict";
	var _obj = false;
	if (_obj === false){
		_obj = new obj();
	}
	return _obj;
})(_client);



//lastTime
(function(client){
	client.component('engineBak',function(){
		"use strict";
		
		_baseComponent.apply(this);
		var _this = this;
		
		this._config = {}
		
		
		var _oldTime = null;
		
		this._timer = {}		
		this._timerConfig = {}
		
		this._lastTimeInterval = function(){
			var _nowTime = (new Date()).getTime();
			var interval = (_nowTime - _oldTime)/1000;
			interval = Math.floor(interval);
			if(interval <= 0){
				interval = 1;
			};
			_oldTime = (new Date()).getTime();
			return interval;
		}
		
		this._construct = function(){
			
		}
		
		this.addTimer = function(timerName,timerConfig){
			this._timerConfig[timerName] = timerConfig;
		}
		//cycle
		this.run = function(){
			for(var timerName in _this._timerConfig){
				if(!_this._timer.hasOwnProperty(timerName)){
					_this._timer[timerName] = setInterval(_this._timerConfig[timerName]['engine'],_this._timerConfig[timerName]['cycle'] * 1000,_this);
				}
			}
			
			
		}
		
		this.config = function(config){
			this._config = config;
			return this;
		}
		
		this.getConfig = function(configName){
			return this._config[configName];
		}
		
		this._construct();
		
	});	
	
	
	
	client.component('engine',function(){
		"use strict";
		_baseComponent.apply(this);
		var _this = this;
		
		this._timerList = {}
		this._timerRunPool = {}
		
		this._rely = {
			'base':'baseEngine',
			'ajax':'baseAjaxEngine',
		}
		
		this.createTimer = function(timerName,cycle,timerType){
			var timerClass = null;
			if(typeof timerType == 'undefined'){
				timerType = 'base';
			}

			if(typeof timerType == 'string'){

				if(_this._rely.hasOwnProperty(timerType)){
					timerClass = _this._rely[timerType];
				}else{
					timerClass = timerType;
				}
			}else{
				timerClass = timerType;
			}
			
			var timerObj = eval('new '+timerClass+'()');
			timerObj.cycle = cycle;
			_this.addTimer(timerName,timerObj);
			return timerObj;
		}
		
		this.createTimerNG = function(timerName,cycle,timerType){
			var timerObj = false;
			if(this.hasTimer(timerName)){
				timerObj = this.selectTimer(timerName);
			}else{
				timerObj = this.createTimer(timerName,cycle,timerType);
			}
			return timerObj;
		}
		
		this.addTimer = function(timerName,engine){
			_this._timerList[timerName] = engine;
		}
		
		this.selectTimer = function(timerName){
			return _this._timerList[timerName];
		}
		
		this.hasTimer = function(timerName){
			this._timerList.hasOwnProperty(timerName);
		}
		
		this.config = function(key,value){
			if(typeof value == 'undefined'){
				return _this.getHash(key);
			}else{
				_this.setHash(key,value);
				return this;
			}
		}
		
		this.run = function(timerList){
			var timerPool = {};
			if(typeof timerList == 'undefined'){
				timerPool = _this._timerList;
			}else{
				for(var timerId in timerList){
					var tempTimerName = timerList[timerId];
					timerPool[tempTimerName] = tempTimerName;
				}
			}
			
			for(var timerName in timerPool){
				clearInterval(_this._timerRunPool[timerName]);
				_this._timerRunPool[timerName] = setInterval(_this.selectTimer(timerName).run,_this.selectTimer(timerName).cycle * 1000);
			}
		}
		
		
	});	
	
})(client);

//shelf namespace
(function(client){
	
	var shelf = function(){
		"use strict";
		_baseEvent.apply(this);

		var _this = this;
		this._auctionPool = {};
		
		this.demand=null;

		this.name = '';	
		this.limit = -1;
		this._baseRules = {
			'data':function(auction){
				return auction.id != 0;
			},
			'unique':function(auction){
				return !_this._auctionPool.hasOwnProperty(auction.id);
			},
			'limit':function(auction){
				if(_this.limit == -1){
					return true;
				}
				var itemNaumber = _this.auctionCount();
				return itemNaumber < _this.limit;
			},
			'runPutOnValidateEvent':function(auction){
				return _this.runPutOnValidateEvent(auction);
			},
			'end_time':function(auction){
				var maxAuctionEndTime = 0;
				for(var auctionId in _this._auctionPool){
					var auctionEndTime = client.auction.get(auctionId).getAttribute('end_time');
					if(maxAuctionEndTime < auctionEndTime){
						maxAuctionEndTime = auctionEndTime;
					}		
				}
				return auction.getAttribute('end_time') >= maxAuctionEndTime;
			}
		}

		this.auctionCount = function(){
			var itemNaumber = 0;
			for(var auctionId in _this._auctionPool){
				itemNaumber += 1;
			}
			return itemNaumber;
		}
		
		this._construct = function(){
			this.setPutOnValidateEvent(function(auction){
				return true;
			})
		}
		
		this.setDispatcherEvent = function(attribute,event){
			return this.setBaseEvent(attribute+'.dispatcher',event);
		}
		
		this.runDispatcherEvent = function(attribute,value,auction){
			return this.runBaseEvent(attribute+'.dispatcher',[attribute,value,auction]);
		}
			
		this.setPutOnEvent = function(event){
			return this.setBaseEvent('putOn',event);
		}
		
		this.runPutOnEvent = function(auction){
			return this.runBaseEvent('putOn',[auction]);
		}
		
		this.setPutOnValidateEvent = function(event){
			return this.setBaseEvent('putOnValidate',event);
		}
		
		this.runPutOnValidateEvent = function(auction){
			return this.runBaseEvent('putOnValidate',[auction]);
		}
		
		this.setPutOffEvent = function(event){
			return this.setBaseEvent('putOff',event);
		}
		
		this.runPutOffEvent = function(auction){
			return this.runBaseEvent('putOff',[auction]);
		}
		
		
		this.addAuctionId = function(auctionId){
			this._auctionPool[auctionId] = auctionId;
		}
		
		this.delAuctionId = function(auctionId){
			delete this._auctionPool[auctionId];
		}
		
		this.clearAuctionId = function(){
			this._auctionPool = {}
		}
		
		this.putOn = function(auction){
			var putOnValid = true;
			for(var rule in this._baseRules){
				if(putOnValid){
					putOnValid = _this._baseRules[rule](auction);
				}			
			}
			if(putOnValid){
				auction.addShelf(this.name);
				this.addAuctionId(auction.id);
				this.runPutOnEvent(auction);
			}
			return this;
		}
		
		this.putOff = function(auction){			
			this.delAuctionId(auction.id);
			this.runPutOffEvent(auction);
		}
		
		this.dispatcher = function(attribute,value,auction){
			return this.runDispatcherEvent(attribute,value,auction);
		}
		
		this.check = function(){		
			//this.limit == -1 ||  
			var shelfInfo = false
			
			
			
			
			if(this.auctionCount() < this.limit){
				shelfInfo = {};
				for(var auctionId in _this._auctionPool){
					var auctionEndTime = client.auction.get(auctionId).getAttribute('end_time');
					if(!shelfInfo.hasOwnProperty('maxEndTime')){
						shelfInfo['maxEndTime'] = auctionEndTime;
					}
					

					if(!shelfInfo.hasOwnProperty('minEndTime')){
						shelfInfo['minEndTime'] = auctionEndTime;
					}
					
					if( auctionEndTime > shelfInfo['maxEndTime']){
						shelfInfo['maxEndTime'] = auctionEndTime;
					}
					
					if(auctionEndTime < shelfInfo['minEndTime']){
						shelfInfo['minEndTime'] = auctionEndTime;
					}					
				}
				
				shelfInfo['cmd'] = 'getAuction';
				shelfInfo['name'] = this.name;
				shelfInfo['demand'] = this.demand;
				shelfInfo['auctionIds'] = this._auctionPool;
				shelfInfo['limit'] = this.limit - this.auctionCount();
			}	
			return shelfInfo;
		}
		
		this.init = function(init){
			if(init.hasOwnProperty('dispatcher')){
				for(var attribute in init['dispatcher']){
					this.setDispatcherEvent(attribute,init['dispatcher'][attribute]);
				}
			}
			
			if(init.hasOwnProperty('putOnValidate')){
				this.setPutOnValidateEvent(init['putOnValidate']);
			}
			
			if(init.hasOwnProperty('limit')){
				this.limit = init['limit'];
			}
			
			if(init.hasOwnProperty('')){
				this.demand=init.demand;
			}
			
		}
		
		
	}


	
	client.component('shelf',function(){
		"use strict";
		_baseHash.apply(this);
		
		this.create = function(name,init){
			var createShelf = new shelf();
			createShelf.name = name;
			createShelf.init(init);
			this.set(name,createShelf)
			return createShelf;
		}
		
		this.set = function(name,shelf){
			return this.setHash(name,shelf);
		}
		
		this.get = function(name){
			return this.getHash(name);
		}
		
		this.has = function(name){
			return this.hasHash(name);
		}
		
		this.all = function(){
			return this.getHashAll();
		}
	});	
	
	
	var shelfDom = function(){
		"use strict";
		shelf.apply(this);
		var _this = this;
		this.auctionTemplate = null;
		this.shelfRoot = function(rootDom){
			if(typeof rootDom == 'undefined'){
				rootDom = this.name;
			}
			return $(rootDom);
		}
		
		
		
		this.selectAuction = function(id){
			return this.shelfRoot().find('[auction-id="'+id+'"]');
		}
		
		this.selectAuctionAttribute = function(selectAuction,attribute){
			return selectAuction.find('[attribute="'+attribute+'"]');
		} 
		
		this.runDispatcherEvent = function(attribute,value,auction){
			var _this = this;
			
			if(this.hasBaseEvent(attribute+'.dispatcher')){
				return this.runBaseEvent(attribute+'.dispatcher',[_this.selectAuction(auction.id),value,attribute,auction]);
			}else{
				return this.runBaseEvent('initShelfDom.dispatcher',[_this.selectAuction(auction.id),value,attribute,auction]);
			}

		}
			
		
		this.setDomAttributeEvent = function(attribute,event){
			return this.setBaseEvent(attribute+'.domAttribute',event);
		}
		
		this.runDomAttributeEvent = function(attribute,auctionDom){			
			if(this.hasBaseEvent(attribute+'.domAttribute')){
				return this.runBaseEvent(attribute+'.domAttribute',[attribute,auctionDom]);
			}else{
				return this.runBaseEvent('initShelfDom.domAttribute',[attribute,auctionDom]);
			}
		}
		
		this.init = function(init){
			var _this = this;
			this.setBaseEvent('initShelfDom.domAttribute',function(attribute,auctionDom){
				var attributeData = auctionDom.attr('attribute-data-'+attribute);
				if(typeof attributeData != 'undefined'){
					return attributeData;
				}else{
					return auctionDom.find('[attribute="'+attribute+'"]').html();
				}
			});
			
			this.setBaseEvent('initShelfDom.dispatcher',function(auctionDom,value,attribute,auction){
				
				if(attribute == 'status'){
					switch(value){
						case '0':
							break;
						case '1':
							break;
						case '2':
							var auctionId = auction.id;
							_this.putOff(auction);
							break;
					}
				}
				
				
				var auctionAttribute = _this.selectAuctionAttribute(auctionDom,attribute);
				if(auctionAttribute.length <= 0){
					return auctionDom.attr('attribute-data-'+attribute,value);
				}else{
					var run = null;
					switch(true){
						case auctionAttribute.is('img'):
							run = auctionAttribute.attr('src',value);
						break;
						case auctionAttribute.is('input'):
							run = auctionAttribute.val(value);
						break;
						default:
							var oldValue = auctionAttribute.html();
							if(attribute == 'winner_price'){
								value = '$'+value;
							}
							run = auctionAttribute.html(value);
							if(oldValue != value){
								var attributeAnimation = auctionAttribute.attr('attribute-animation');
								if(typeof attributeAnimation != 'undefined'){
									auctionAttribute.addClass(attributeAnimation);
									var attributeTime = auctionAttribute.attr('animation-time');
									var animation = setTimeout(function(){
										auctionAttribute.removeClass(attributeAnimation);
									},attributeTime*1000);
								}
							}
							
							
						break;
					}
					return run;
				}
								
				
				
			});
			
			
			if(init.hasOwnProperty('dispatcher')){
				for(var attribute in init['dispatcher']){
					this.setDispatcherEvent(attribute,init['dispatcher'][attribute]);
				}
			}
			
			if(init.hasOwnProperty('putOnValidate')){
				this.setPutOnValidateEvent(init['putOnValidate']);
			}
			
			if(init.hasOwnProperty('limit')){
				this.limit = init['limit'];
			}
			
			if(init.hasOwnProperty('demand')){
				this.demand = init['demand'];
			}
			
			if(init.hasOwnProperty('auctionTemplate')){
				this.auctionTemplate = init['auctionTemplate'];
			}
			
			
			if(init.hasOwnProperty('domAttribute')){
				for(var attribute in init['domAttribute']){
					this.setDispatcherEvent(attribute,init['domAttribute'][attribute]);
				}
			}
		}
		
		this.setPutOnEvent(function(auction){
			var auctionId = auction.id;
			if(_this.selectAuction(auctionId).length <= 0){
				var auctionHtml = $(_this.auctionTemplate);	
				auctionHtml.attr('auction-id',auctionId);
				_this.shelfRoot().append(auctionHtml);
				auction.setAttributes(auction.getAttributes());
			}
			
		});
		
		
		this.setPutOffEvent(function(auction){			
			//_this.selectAuction(auction.id).find('.auction-status-show').show();
			_this.selectAuctionAttribute(_this.selectAuction(auction.id),'last_date').html('End');
			_this.selectAuction(auction.id).find('.btn-bid-auction').attr('disabled',true);			
			_this.selectAuction(auction.id).attr('auction-status',2);
			/*
			var _timeout = setTimeout(function(){
				_this.selectAuction(auction.id).remove();
			},10000);
			

			if(_this.limit == -1 ||  _this.auctionCount() < _this.limit){
				
			}
			
			console.log(_this.auctionCount());
			*/
		
		});
		
		this.buildHtml = function(dom){
			this.clearAuctionId();
			//client.auction.clear();
			var _this = this;
			if(typeof dom == 'undefined'){
				dom = this.shelfRoot();
			}
			dom.find('[auction-id]').each(function(){
				var auctionDom =  $(this);
				var auctionId = auctionDom.attr('auction-id');
				var auctionData = {'id':auctionId}
				var auctionObj = client.auction.createNG({'id':auctionId});
				var auctionAttributes = auctionObj.getAttributes();
				for(var attribute in auctionAttributes){
					auctionData[attribute] = _this.runDomAttributeEvent(attribute,auctionDom);
				}
				auctionObj.setAttributes(auctionData);
				_this.putOn(auctionObj);
			});
		}
		
	}

	client.component('shelfDom',function(){
		"use strict";
		_baseHash.apply(this);
		var _this = this;
		this._construct = function(){
			client.engine.addTimer('engineShelf',{
				'engine':_this.engineShelf,
				'cycle':10
			});
		}
		
		this.create = function(name,init){
			var createShelf = new shelfDom();
			createShelf.name = name;
			createShelf.init(init);
			this.set(name,createShelf)
			return createShelf;
		}
		
		this.set = function(name,shelf){
			return this.setHash(name,shelf);
		}
		
		this.get = function(name){
			return this.getHash(name);
		}
		
		this.has = function(name){
			return this.hasHash(name);
		}
		
		this.all = function(){
			return this.getHashAll();
		}
		
		this.engineShelf = function(){
			
		}
		
		this.engineShelf = function(clientEngine){
			var shelfs = _this.all();
			var request = false;
			console.log(shelfs);
			for(var shelf in shelfs){
				var cmd =  shelfs[shelf].check();
				if(cmd){
					if(request === false){
						request = {}
					}
					request[shelf] = cmd;
				}
				
			}
			if(request !== false){
				$.ajax({ 
					'url':clientEngine.getConfig('engineShelf').url,
					'data':{
						'request':request
					},
					'type':'post',
					'dataType':'json',
					'success':function(response){	
						$('[auction-status="2"]').remove();
						for(var shelfName in response){
							if(response[shelfName].hasOwnProperty('rData')){
								

								for(var rDataId in response[shelfName]['rData']){
									var auctionId =  response[shelfName]['rData'][rDataId].auction_id;
									var auctionObj = client.auction.createNG({'id':auctionId});
									var auctionData = response[shelfName]['rData'][rDataId];
									auctionObj.setAttributes(auctionData);
									_this.get(shelfName).putOn(auctionObj);
								}
							}
							
						}
						
					},
					'complete':function(){
					}
				});
			}
			
			
		}
		
		this._construct();
	});	
	
	jQuery.fn.shelf = function(init){
		var _this = $(this);
		var _shelfName = _this.selector;
		if(typeof init != 'undefined'){
			client.shelfDom.create(_shelfName,init);
		}
		return client.shelfDom.get(_shelfName);
	}
	
})(client);

//auction namespace
(function(client){
	
	var auctionDom = function(){
		
	}
	
	var auctionDispatcher = function(){
		
	}
	
	var shelf = function(){
		"use strict";
		_baseComponent.apply(this);
		var _this = this;
		
		this.setAuctionId = function(auctionId){
			_this.setHash(auctionId,auctionId);
		}
		
		this.delAuctionId = function(auctionId){
			_this.delHash(auctionId);
		}
		
		this.putOn = function(auction){
			var putOnValid = true;
			for(var rule in this._baseRules){
				if(putOnValid){
					putOnValid = _this._baseRules[rule](auction);
				}			
			}
			if(putOnValid){
				auction.addShelf(this.name);
				this.addAuctionId(auction.id);
				this.runPutOnEvent(auction);
			}
			return this;
		}
		
		this.putOff = function(auction){
			var putOnValid = true;
			for(var rule in this._baseRules){
				if(putOnValid){
					putOnValid = _this._baseRules[rule](auction);
				}			
			}
			if(putOnValid){
				auction.addShelf(this.name);
				this.addAuctionId(auction.id);
				this.runPutOnEvent(auction);
			}
			return this;
		}
		
	}
	
	var shelf = function(){
		"use strict";
		_baseEvent.apply(this);

		var _this = this;
		this._auctionPool = {};
		
		this.demand=null;

		this.name = '';	
		this.limit = -1;
		this._baseRules = {
			'data':function(auction){
				return auction.id != 0;
			},
			'unique':function(auction){
				return !_this._auctionPool.hasOwnProperty(auction.id);
			},
			'limit':function(auction){
				if(_this.limit == -1){
					return true;
				}
				var itemNaumber = _this.auctionCount();
				return itemNaumber < _this.limit;
			},
			'runPutOnValidateEvent':function(auction){
				return _this.runPutOnValidateEvent(auction);
			},
			'end_time':function(auction){
				var maxAuctionEndTime = 0;
				for(var auctionId in _this._auctionPool){
					var auctionEndTime = client.auction.get(auctionId).getAttribute('end_time');
					if(maxAuctionEndTime < auctionEndTime){
						maxAuctionEndTime = auctionEndTime;
					}		
				}
				return auction.getAttribute('end_time') >= maxAuctionEndTime;
			}
		}

		this.auctionCount = function(){
			var itemNaumber = 0;
			for(var auctionId in _this._auctionPool){
				itemNaumber += 1;
			}
			return itemNaumber;
		}
		
		this._construct = function(){
			this.setPutOnValidateEvent(function(auction){
				return true;
			})
		}
		
		this.setDispatcherEvent = function(attribute,event){
			return this.setBaseEvent(attribute+'.dispatcher',event);
		}
		
		this.runDispatcherEvent = function(attribute,value,auction){
			return this.runBaseEvent(attribute+'.dispatcher',[attribute,value,auction]);
		}
			
		this.setPutOnEvent = function(event){
			return this.setBaseEvent('putOn',event);
		}
		
		this.runPutOnEvent = function(auction){
			return this.runBaseEvent('putOn',[auction]);
		}
		
		this.setPutOnValidateEvent = function(event){
			return this.setBaseEvent('putOnValidate',event);
		}
		
		this.runPutOnValidateEvent = function(auction){
			return this.runBaseEvent('putOnValidate',[auction]);
		}
		
		this.setPutOffEvent = function(event){
			return this.setBaseEvent('putOff',event);
		}
		
		this.runPutOffEvent = function(auction){
			return this.runBaseEvent('putOff',[auction]);
		}
		
		
		this.addAuctionId = function(auctionId){
			this._auctionPool[auctionId] = auctionId;
		}
		
		this.delAuctionId = function(auctionId){
			delete this._auctionPool[auctionId];
		}
		
		this.clearAuctionId = function(){
			this._auctionPool = {}
		}
		
		this.putOn = function(auction){
			var putOnValid = true;
			for(var rule in this._baseRules){
				if(putOnValid){
					putOnValid = _this._baseRules[rule](auction);
				}			
			}
			if(putOnValid){
				auction.addShelf(this.name);
				this.addAuctionId(auction.id);
				this.runPutOnEvent(auction);
			}
			return this;
		}
		
		this.putOff = function(auction){			
			this.delAuctionId(auction.id);
			this.runPutOffEvent(auction);
		}
		
		this.dispatcher = function(attribute,value,auction){
			return this.runDispatcherEvent(attribute,value,auction);
		}
		
		this.check = function(){		
			//this.limit == -1 ||  
			var shelfInfo = false
			
			
			
			
			if(this.auctionCount() < this.limit){
				shelfInfo = {};
				for(var auctionId in _this._auctionPool){
					var auctionEndTime = client.auction.get(auctionId).getAttribute('end_time');
					if(!shelfInfo.hasOwnProperty('maxEndTime')){
						shelfInfo['maxEndTime'] = auctionEndTime;
					}
					

					if(!shelfInfo.hasOwnProperty('minEndTime')){
						shelfInfo['minEndTime'] = auctionEndTime;
					}
					
					if( auctionEndTime > shelfInfo['maxEndTime']){
						shelfInfo['maxEndTime'] = auctionEndTime;
					}
					
					if(auctionEndTime < shelfInfo['minEndTime']){
						shelfInfo['minEndTime'] = auctionEndTime;
					}					
				}
				
				shelfInfo['cmd'] = 'getAuction';
				shelfInfo['name'] = this.name;
				shelfInfo['demand'] = this.demand;
				shelfInfo['auctionIds'] = this._auctionPool;
				shelfInfo['limit'] = this.limit - this.auctionCount();
			}	
			return shelfInfo;
		}
		
		this.init = function(init){
			if(init.hasOwnProperty('dispatcher')){
				for(var attribute in init['dispatcher']){
					this.setDispatcherEvent(attribute,init['dispatcher'][attribute]);
				}
			}
			
			if(init.hasOwnProperty('putOnValidate')){
				this.setPutOnValidateEvent(init['putOnValidate']);
			}
			
			if(init.hasOwnProperty('limit')){
				this.limit = init['limit'];
			}
			
			if(init.hasOwnProperty('')){
				this.demand=init.demand;
			}
			
		}
		
		
	}
	
	
	
	
	
	
	
	var auctionBroadcasts = function(){
		"use strict";
		this.broadcastsList = {}
		
		auctionBroadcasts.prototype.addBroadcastsList = function(sendName,dispatcher){
			this.broadcastsList[sendName] = dispatcher;
		}
		
		auctionBroadcasts.prototype.send = function(attribute,value,auctionDataObj){
			var broadcastsList =  this.broadcastsList;
			for(var broadcast in broadcastsList){
				broadcastsList[broadcast].dispatcher(attribute,value,auctionDataObj);
			}
		}
	}
	
	var auctionData = function(auctionId){
		"use strict";
		_baseEvent.apply(this);
		var _this = this;
		this.id = 0;
		this.attributes = {
			'title':		'',
			'winner_price':	'',
			'winner_name':	'',
			'winner_avater':'',
			'last_time':	0,
			'last_date':	0,
			'end_time':		0,
			'status':		0,
		}
		this._broadcasts = false;
		
		auctionData.prototype._construct = function(auctionId){
			if(typeof auctionId != 'undefined'){
				this.id = auctionId;
			}
			
		}
		
		auctionData.prototype.init = function(init){
			
		}
		
		auctionData.prototype.broadcasts = function(){
			if(this._broadcasts === false){
				this._broadcasts =  new auctionBroadcasts();
			}
			return this._broadcasts;
		}
		
		auctionData.prototype.setAttribute = function(attribute,value){
			if(this.attributes.hasOwnProperty(attribute)){
				if(_this.hasBaseEvent(attribute+'.setFormat')){
					_this.attributes[attribute] = _this.runBaseEvent(attribute+'.setFormat',[attribute,value]);
				}else{
					_this.attributes[attribute] = value;
				}	
				this.broadcasts().send(attribute,_this.attributes[attribute],_this);
				this.runBaseEvent('setAttributeAfter',[attribute,_this.attributes[attribute],_this]);
			}
		}
		
		auctionData.prototype.getAttribute = function(attribute){
			return this.attributes[attribute];
		}
		
		auctionData.prototype.setAttributes = function(attributes){
			for(var attribute in attributes){
				this.setAttribute(attribute,attributes[attribute]);
			}
		}
		
		auctionData.prototype.getAttributes = function(attribute){
			return this.attributes;
		}
				
		auctionData.prototype.runLastTime = function(interval){
			this.updateLastTime(this.getAttribute('last_time') - interval);
		}
		
		auctionData.prototype.severLastTime = function(serverTime){
			this.updateLastTime(this.getAttribute('end_time') - serverTime);
		}
		
		auctionData.prototype.updateLastTime = function(lastTime){
			this.setAttribute('last_time',lastTime);
			if(this.getAttribute('last_time') <= 20){
				var last_Date = '<span class="critical">'+this._buildLastDate(this.getAttribute('last_time'))+'</span>';
			}else{
				var last_Date = this._buildLastDate(this.getAttribute('last_time'));
			}
			
			this.setAttribute('last_date',last_Date);
		}
		
		auctionData.prototype._buildLastDate = function(lastTime){
			var timeString = {
				'h':0,
				'i':0,
				's':0
			};
			
			if (lastTime> 0) {   
				timeString['d'] = Math.floor((lastTime / 3600) / 24);       //d 
				timeString['h'] = Math.floor((lastTime / 3600) % 24);      //h    
				timeString['m'] = Math.floor((lastTime / 60) % 60);      //m 
				timeString['s'] = Math.floor(lastTime % 60);             //s
				if(timeString['h'] < 10){
					timeString['h'] = '0' + timeString['h'];
				}
				if(timeString['m'] < 10){
					timeString['m'] = '0' + timeString['m'];
				}
				if(timeString['s'] < 10){
					timeString['s'] = '0' + timeString['s'];
				}
				
				if(timeString['d'] > 1){
					timeString['d'] += " days ";
			   	}else{
			   		timeString['d'] = '';
			   	}			
			   	return timeString['d'] + timeString['h'] + ":" + timeString['m'] + ":" + timeString['s'];
			} else {
				return '00:00:00';
			}
		}
		
		this._construct();
	}
	
	client.component('auction',function(){
		"use strict";
		_baseComponent.apply(this);
		var _this = this;
		
		this._construct = function(){
			client.engine.createTimerNG('engineAuctionInfo',1,'ajax').setRequest({ 
				'data':{
					'ids':function(){
						var auctions = _this.all();
						var auctionIds = [];
						for(var auction in auctions){
							auctionIds.push(auction);
						}
						auctionIds = auctionIds.join('|');
						return auctionIds;
					},
					'ver':'111',
				},
				'type':'post',
				'dataType':'json',
				'success':function(response){
					console.log('asfasdf');
				},
				'complete':function(){
				}
			}).setBaseEvent('ajax.request.before',function(engine){
				var url = client.engine.config('engineAuctionInfo.url');
				engine.setRequest({'url':url});
				return true;
			}).setResponseEvent('tey1',function(){
				console.log('asfasfasd111');
			});
			
		}
		
		this.create = function(auctionData){
			if(auctionData.hasOwnProperty('id')){
				var createAuction = new auction(auctionData.id);
				createAuction.setAttributes(auctionData);
				this.set(createAuction.id,createAuction);
			}
			return createAuction;
		}
		
		this.createNG = function(auctionData){
			var auctionObj = false;
			if(auctionData.hasOwnProperty('id')){
				if(this.has(auctionData.id)){
					auctionObj = this.get(auctionData.id);
				}else{
					auctionObj = _this.create(auctionData);
				}
			}
			return auctionObj;
		}
		
		this.set = function(id,auction){
			return this.setHash(id,auction);
		}
		
		this.get = function(id){
			return this.getHash(id);
		}
		
		this.has = function(id){
			return this.hasHash(id);
		}
		
		this.clear = function(){
			return this.clearHash();
		}
		
		this.del = function(id){
			return this.delHash(id);
		}
		
		this.all = function(){
			return this.getHashAll();
		}

		this._construct();
		
		
		
		/*
		
		
		this._construct = function(){
			client.engine.addTimer('engineAuctionInfo',{
				'engine':_this.engineAuctionInfo,
				'cycle':1
			});
		}
		
		
		this.clientDataVer = null;
		this.create = function(init){
			if(init.hasOwnProperty('id')){
				var createAuction = new auction(init.id);
				createAuction.setAttributes(init);
				this.set(createAuction.id,createAuction);
			}
			return createAuction;
		}
		
		this.createNG = function(init){
			var auctionObj = false;
			if(init.hasOwnProperty('id')){
				if(this.has(init.id)){
					auctionObj = this.get(init.id);
				}else{
					var auctionObj = new auction(init.id);
					auctionObj.setAttributes(init);
					this.set(auctionObj.id,auctionObj);
				}
			}
			return auctionObj;
		}
		
		
		
		this.engineAuctionInfo = function(clientEngine){
			
			$.ajax({ 
				'url':clientEngine.getConfig('engineAuctionInfo').url,
				'data':{
					'ids':auctionIds,
					'ver':_this.clientDataVer
				},
				'type':'post',
				'dataType':'json',
				'success':function(response){					
					if(response.hasOwnProperty('ver')){
						_this.clientDataVer = response.ver;
					}
					if(response.hasOwnProperty('rData')){
						for(var rDataId in response.rData){
							if(_this.has(response.rData[rDataId].auction_id)){
								_this.get(response.rData[rDataId].auction_id).setAttributes(response.rData[rDataId]);
							}
							
						}
					}
					if(response.hasOwnProperty('t')){
						for(var auction in auctions){
							auctions[auction].severLastTime(response.t);
						}
					}
					
					
				},
				'complete':function(){
				}
			});
			
		}

		this._construct();
		*/
	});
	
})(client);

//
//(function(client){
//	
//	var detail = function(){
//		"use strict";
//		_baseEvent.apply(this);
//
//		var _this = this;
//		this._auctionPool = {};
//		
//		this.demand=null;
//
//		this.name = '';	
//		this.limit = 1;
//		this._baseRules = {
//			'data':function(auction){
//				return auction.id != 0;
//			},
//			'unique':function(auction){
//				return !_this._auctionPool.hasOwnProperty(auction.id);
//			},
//			'limit':function(auction){
//				if(_this.limit == 1){
//					return true;
//				}
//				var itemNaumber = _this.auctionCount();
//				return itemNaumber < _this.limit;
//			},
//			'runPutOnValidateEvent':function(auction){
//				return _this.runPutOnValidateEvent(auction);
//			},
//			'end_time':function(auction){
//				var maxAuctionEndTime = 0;
//				for(var auctionId in _this._auctionPool){
//					var auctionEndTime = client.auction.get(auctionId).getAttribute('end_time');
//					if(maxAuctionEndTime < auctionEndTime){
//						maxAuctionEndTime = auctionEndTime;
//					}		
//				}
//				return auction.getAttribute('end_time') >= maxAuctionEndTime;
//			}
//		}
//
//		this.auctionCount = function(){
//			var itemNaumber = 0;
//			for(var auctionId in _this._auctionPool){
//				itemNaumber += 1;
//			}
//			return itemNaumber;
//		}
//		
//		this._construct = function(){
//			this.setPutOnValidateEvent(function(auction){
//				return true;
//			})
//		}
//		
//		this.setDispatcherEvent = function(attribute,event){
//			return this.setBaseEvent(attribute+'.dispatcher',event);
//		}
//		
//		this.runDispatcherEvent = function(attribute,value,auction){
//			return this.runBaseEvent(attribute+'.dispatcher',[attribute,value,auction]);
//		}
//			
//		this.setPutOnEvent = function(event){
//			return this.setBaseEvent('putOn',event);
//		}
//		
//		this.runPutOnEvent = function(auction){
//			return this.runBaseEvent('putOn',[auction]);
//		}
//		
//		this.setPutOnValidateEvent = function(event){
//			return this.setBaseEvent('putOnValidate',event);
//		}
//		
//		this.runPutOnValidateEvent = function(auction){
//			return this.runBaseEvent('putOnValidate',[auction]);
//		}
//		
//		this.setPutOffEvent = function(event){
//			return this.setBaseEvent('putOff',event);
//		}
//		
//		this.runPutOffEvent = function(auction){
//			return this.runBaseEvent('putOff',[auction]);
//		}
//		
//		
//		this.addAuctionId = function(auctionId){
//			this._auctionPool[auctionId] = auctionId;
//		}
//		
//		this.delAuctionId = function(auctionId){
//			delete this._auctionPool[auctionId];
//		}
//		
//		this.clearAuctionId = function(){
//			this._auctionPool = {}
//		}
//		
//		this.putOn = function(auction){
//			var putOnValid = true;
//			for(var rule in this._baseRules){
//				if(putOnValid){
//					putOnValid = _this._baseRules[rule](auction);
//				}			
//			}
//			if(putOnValid){
//				auction.addShelf(this.name);
//				this.addAuctionId(auction.id);
//				this.runPutOnEvent(auction);
//			}
//			return this;
//		}
//		
//		this.putOff = function(auction){			
//			this.delAuctionId(auction.id);
//			this.runPutOffEvent(auction);
//		}
//		
//		this.dispatcher = function(attribute,value,auction){
//			return this.runDispatcherEvent(attribute,value,auction);
//		}
//		
//		this.check = function(){		
//			//this.limit == -1 ||  
//			var shelfInfo = false
//			
//			
//			
//			
//			if(this.auctionCount() < this.limit){
//				shelfInfo = {};
//				for(var auctionId in _this._auctionPool){
//					var auctionEndTime = client.auction.get(auctionId).getAttribute('end_time');
//					if(!shelfInfo.hasOwnProperty('maxEndTime')){
//						shelfInfo['maxEndTime'] = auctionEndTime;
//					}
//					
//
//					if(!shelfInfo.hasOwnProperty('minEndTime')){
//						shelfInfo['minEndTime'] = auctionEndTime;
//					}
//					
//					if( auctionEndTime > shelfInfo['maxEndTime']){
//						shelfInfo['maxEndTime'] = auctionEndTime;
//					}
//					
//					if(auctionEndTime < shelfInfo['minEndTime']){
//						shelfInfo['minEndTime'] = auctionEndTime;
//					}					
//				}
//				
//				shelfInfo['cmd'] = 'getAuction';
//				shelfInfo['name'] = this.name;
//				shelfInfo['demand'] = this.demand;
//				shelfInfo['auctionIds'] = this._auctionPool;
//				shelfInfo['limit'] = this.limit - this.auctionCount();
//			}	
//			return shelfInfo;
//		}
//		
//		this.init = function(init){
//			if(init.hasOwnProperty('dispatcher')){
//				for(var attribute in init['dispatcher']){
//					this.setDispatcherEvent(attribute,init['dispatcher'][attribute]);
//				}
//			}
//			
//			if(init.hasOwnProperty('putOnValidate')){
//				this.setPutOnValidateEvent(init['putOnValidate']);
//			}
//			
//			if(init.hasOwnProperty('limit')){
//				this.limit = init['limit'];
//			}
//			
//			if(init.hasOwnProperty('demand')){
//				this.demand=init.demand;
//			}
//			
//		}
//		
//		
//	}
//
//
//	
//	client.component('shelf',function(){
//		"use strict";
//		_baseHash.apply(this);
//		
//		this.create = function(name,init){
//			var createShelf = new shelf();
//			createShelf.name = name;
//			createShelf.init(init);
//			this.set(name,createShelf)
//			return createShelf;
//		}
//		
//		this.set = function(name,shelf){
//			return this.setHash(name,shelf);
//		}
//		
//		this.get = function(name){
//			return this.getHash(name);
//		}
//		
//		this.has = function(name){
//			return this.hasHash(name);
//		}
//		
//		this.all = function(){
//			return this.getHashAll();
//		}
//	});	
//	
//	
//	var shelfDom = function(){
//		"use strict";
//		shelf.apply(this);
//		var _this = this;
//		this.auctionTemplate = null;
//		this.shelfRoot = function(rootDom){
//			if(typeof rootDom == 'undefined'){
//				rootDom = this.name;
//			}
//			return $(rootDom);
//		}
//		
//		
//		
//		this.selectAuction = function(id){
//			return this.shelfRoot().find('[auction-id="'+id+'"]');
//		}
//		
//		this.selectAuctionAttribute = function(selectAuction,attribute){
//			return selectAuction.find('[attribute="'+attribute+'"]');
//		} 
//		
//		this.runDispatcherEvent = function(attribute,value,auction){
//			var _this = this;
//			
//			if(this.hasBaseEvent(attribute+'.dispatcher')){
//				return this.runBaseEvent(attribute+'.dispatcher',[_this.selectAuction(auction.id),value,attribute,auction]);
//			}else{
//				return this.runBaseEvent('initShelfDom.dispatcher',[_this.selectAuction(auction.id),value,attribute,auction]);
//			}
//
//		}
//			
//		
//		this.setDomAttributeEvent = function(attribute,event){
//			return this.setBaseEvent(attribute+'.domAttribute',event);
//		}
//		
//		this.runDomAttributeEvent = function(attribute,auctionDom){			
//			if(this.hasBaseEvent(attribute+'.domAttribute')){
//				return this.runBaseEvent(attribute+'.domAttribute',[attribute,auctionDom]);
//			}else{
//				return this.runBaseEvent('initShelfDom.domAttribute',[attribute,auctionDom]);
//			}
//		}
//		
//		this.init = function(init){
//			var _this = this;
//			this.setBaseEvent('initShelfDom.domAttribute',function(attribute,auctionDom){
//				var attributeData = auctionDom.attr('attribute-data-'+attribute);
//				if(typeof attributeData != 'undefined'){
//					return attributeData;
//				}else{
//					return auctionDom.find('[attribute="'+attribute+'"]').html();
//				}
//			});
//			
//			this.setBaseEvent('initShelfDom.dispatcher',function(auctionDom,value,attribute,auction){
//				
//				if(attribute == 'status'){
//					switch(value){
//						case '0':
//							break;
//						case '1':
//							break;
//						case '2':
//							var auctionId = auction.id;
//							_this.putOff(auction);
//							break;
//					}
//				}
//				
//				
//				var auctionAttribute = _this.selectAuctionAttribute(auctionDom,attribute);
//				if(auctionAttribute.length <= 0){
//					return auctionDom.attr('attribute-data-'+attribute,value);
//				}else{
//					var run = null;
//					switch(true){
//						case auctionAttribute.is('img'):
//							run = auctionAttribute.attr('src',value);
//						break;
//						case auctionAttribute.is('input'):
//							run = auctionAttribute.val(value);
//						break;
//						default:
//							var oldValue = auctionAttribute.html();
//							if(attribute == 'winner_price'){
//								value = '$'+value;
//							}
//							run = auctionAttribute.html(value);
//							if(oldValue != value){
//								var attributeAnimation = auctionAttribute.attr('attribute-animation');
//								if(typeof attributeAnimation != 'undefined'){
//									auctionAttribute.addClass(attributeAnimation);
//									var attributeTime = auctionAttribute.attr('animation-time');
//									var animation = setTimeout(function(){
//										auctionAttribute.removeClass(attributeAnimation);
//									},attributeTime*1000);
//								}
//							}
//							
//							
//						break;
//					}
//					return run;
//				}
//								
//				
//				
//			});
//			
//			
//			if(init.hasOwnProperty('dispatcher')){
//				for(var attribute in init['dispatcher']){
//					this.setDispatcherEvent(attribute,init['dispatcher'][attribute]);
//				}
//			}
//			
//			if(init.hasOwnProperty('putOnValidate')){
//				this.setPutOnValidateEvent(init['putOnValidate']);
//			}
//			
//			if(init.hasOwnProperty('limit')){
//				this.limit = init['limit'];
//			}
//			
//			if(init.hasOwnProperty('demand')){
//				this.demand = init['demand'];
//			}
//			
//			if(init.hasOwnProperty('auctionTemplate')){
//				this.auctionTemplate = init['auctionTemplate'];
//			}
//			
//			
//			if(init.hasOwnProperty('domAttribute')){
//				for(var attribute in init['domAttribute']){
//					this.setDispatcherEvent(attribute,init['domAttribute'][attribute]);
//				}
//			}
//		}
//		
//		this.setPutOnEvent(function(auction){
//			var auctionId = auction.id;
//			if(_this.selectAuction(auctionId).length <= 0){
//				var auctionHtml = $(_this.auctionTemplate);	
//				auctionHtml.attr('auction-id',auctionId);
//				_this.shelfRoot().append(auctionHtml);
//				auction.setAttributes(auction.getAttributes());
//			}
//			
//		});
//		
//		
//		this.setPutOffEvent(function(auction){			
//			//_this.selectAuction(auction.id).find('.auction-status-show').show();
//			_this.selectAuctionAttribute(_this.selectAuction(auction.id),'last_date').html('End');
//			_this.selectAuction(auction.id).find('.btn-bid-auction').attr('disabled',true);			
//			_this.selectAuction(auction.id).attr('auction-status',2);
//			/*
//			var _timeout = setTimeout(function(){
//				_this.selectAuction(auction.id).remove();
//			},10000);
//			
//
//			if(_this.limit == -1 ||  _this.auctionCount() < _this.limit){
//				
//			}
//			
//			console.log(_this.auctionCount());
//			*/
//		
//		});
//		
//		this.buildHtml = function(dom){
//			this.clearAuctionId();
//			//client.auction.clear();
//			var _this = this;
//			if(typeof dom == 'undefined'){
//				dom = this.shelfRoot();
//			}
//			dom.find('[auction-id]').each(function(){
//				var auctionDom =  $(this);
//				var auctionId = auctionDom.attr('auction-id');
//				var auctionData = {'id':auctionId}
//				var auctionObj = client.auction.createNG({'id':auctionId});
//				var auctionAttributes = auctionObj.getAttributes();
//				for(var attribute in auctionAttributes){
//					auctionData[attribute] = _this.runDomAttributeEvent(attribute,auctionDom);
//				}
//				auctionObj.setAttributes(auctionData);
//				_this.putOn(auctionObj);
//			});
//		}
//		
//	}
//
//	client.component('shelfDom',function(){
//		"use strict";
//		_baseHash.apply(this);
//		var _this = this;
//		this._construct = function(){
//			client.engine.addTimer('engineShelf',{
//				'engine':_this.engineShelf,
//				'cycle':10
//			});
//		}
//		
//		this.create = function(name,init){
//			var createShelf = new shelfDom();
//			createShelf.name = name;
//			createShelf.init(init);
//			this.set(name,createShelf)
//			return createShelf;
//		}
//		
//		this.set = function(name,shelf){
//			return this.setHash(name,shelf);
//		}
//		
//		this.get = function(name){
//			return this.getHash(name);
//		}
//		
//		this.has = function(name){
//			return this.hasHash(name);
//		}
//		
//		this.all = function(){
//			return this.getHashAll();
//		}
//		
//		this.engineShelf = function(){
//			
//		}
//		
//		this.engineShelf = function(clientEngine){
//			var shelfs = _this.all();
//			var request = false;
//			console.log(shelfs);
//			for(var shelf in shelfs){
//				var cmd =  shelfs[shelf].check();
//				if(cmd){
//					if(request === false){
//						request = {}
//					}
//					request[shelf] = cmd;
//				}
//				
//			}
//			if(request !== false){
//				$.ajax({ 
//					'url':clientEngine.getConfig('engineShelf').url,
//					'data':{
//						'request':request
//					},
//					'type':'post',
//					'dataType':'json',
//					'success':function(response){	
//						$('[auction-status="2"]').remove();
//						for(var shelfName in response){
//							if(response[shelfName].hasOwnProperty('rData')){
//								
//
//								for(var rDataId in response[shelfName]['rData']){
//									var auctionId =  response[shelfName]['rData'][rDataId].auction_id;
//									var auctionObj = client.auction.createNG({'id':auctionId});
//									var auctionData = response[shelfName]['rData'][rDataId];
//									auctionObj.setAttributes(auctionData);
//									_this.get(shelfName).putOn(auctionObj);
//								}
//							}
//							
//						}
//						
//					},
//					'complete':function(){
//					}
//				});
//			}
//			
//			
//		}
//		
//		this._construct();
//	});	
//	
//	jQuery.fn.shelf = function(init){
//		var _this = $(this);
//		var _shelfName = _this.selector;
//		if(typeof init != 'undefined'){
//			client.shelfDom.create(_shelfName,init);
//		}
//		return client.shelfDom.get(_shelfName);
//	}
//	
//})(client);
